package com.example.jdk8.threads;

import java.util.concurrent.TimeUnit;

public class ThreadByRunnable {
    public static void main(String[] args) {
        /**
         * Hello: main
         * Done!
         * Hello: Thread-0
         */
        Runnable runnable = () -> {
            String name = Thread.currentThread().getName();
            System.out.println("Hello: " + name);
        };
        runnable.run();
        Thread thread = new Thread(runnable);
        thread.start();
        System.out.println("Done!");
        if (!thread.isInterrupted()) {
            thread.interrupt();
        }
        System.out.println("=================并发处理==================");
        /**
         * 并发程序：模拟长时间运行的任务
         * Foo: main
         * Bar: main
         * Foo: Thread-1
         * Bar: Thread-1
         */
        Runnable task = () -> {
            try {
                String name = Thread.currentThread().getName();
                System.out.println("Foo: " + name);
                TimeUnit.SECONDS.sleep(1); //休眠1 s
                System.out.println("Bar: " + name);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        };
        task.run();
        new Thread(task).start();
    }
}
